package First;

import java.util.Scanner;

public class MobileBusinessHall {
    public  CardUtil cardUtil;
    public Scanner scanner;
    public String currentUser; // 当前登录用户卡号
    public MobileCard mobileCard;
    public MobileBusinessHall() {
        this.cardUtil = new CardUtil();
        this.scanner = new Scanner(System.in);
        // 初始化测试数据
        cardUtil.initScene();
        cardUtil.input();
    }
    // 主菜单
    public void mainMenu() {
        String choice;
        do {
            System.out.println("o(=•ェ•=)o 欢迎使用喵呜5G移动业务大厅 o(=•ェ•=)o");
            System.out.println("1.用户登录");
            System.out.println("2.用户注册 ");
            System.out.println("3.使用兜兜5G");
            System.out.println("4.话费充值");
            System.out.println("5.资费说明");
            System.out.println("6.退出系统");
            System.out.print("请选择:");
            try {
                 choice=scanner.nextLine().trim();
            } catch (NumberFormatException e) {
                e.printStackTrace();
                System.out.println("输入无效，请输入数字1-6！");
                continue;
            }
            switch (choice) {
                case "1":
                    userLogin();
                    break;
                case "2":
                    userRegister();
                    break;
                case "3":
                    System.out.println("请输入您的卡号:");
                    String n=scanner.nextLine().trim();
                    cardUtil.useSoso(n);
                    cardUtil.output();
                    continue;
                case "4":
                    chargeMoney();
                    break;
                case "5":
                    showDescription();
                    break;
                case "6":
                    System.out.println("感谢使用喵呜5G移动业务，再见！");
                    return;
                default:
                    System.out.println("输入无效,请输入数字1-6！");
            }
        } while (true);
    }
    // 用户登录
    public MobileCard userLogin() {
        System.out.println("o(=•ェ•=)m 欢迎登录 o(=•ェ•=)m");
        System.out.print("请输入卡号:");
        String number = scanner.nextLine().trim();
        System.out.print("请输入密码:");
        String password = scanner.nextLine().trim();
        if (cardUtil.isExistCard(number, password)) {
            System.out.println("成功登录,欢迎您~");
            currentUser = number;  // 这里设置当前登录用户
            userMenu();
            return cardUtil.cards.get(number);
        } else {
            System.out.println("sorry~您的账号或密码错误(●'◡'●)");
            return null;
        }
    }
    // 用户菜单(二级菜单)
    public void userMenu() {
        do {
            System.out.println("\no(=•ェ•=)o欢迎使用喵呜5G用户业务o(=•ェ•=)o");
            System.out.println("1.本月账单查询");
            System.out.println("2.套餐余量查询");
            System.out.println("3.打印消费详情");
            System.out.println("4.套餐变更");
            System.out.println("5.办理退网");
            System.out.print("请选择(输入1-5选择功能,其他键返回上一级):");
            String input = scanner.nextLine().trim();
            if (input.length()!=1||input.charAt(0)<'1'||input.charAt(0)>'5') {
                //判断输入的是否为正确的1-5
                currentUser = null;
                return; // 返回上一级
            }
            try {
                //String choice1=scanner.next(input);
                int choice = Integer.parseInt(input);
                switch (choice) {
                    case 1:
                        cardUtil.showAmountDetail(currentUser);
                        break;
                    case 2:
                        cardUtil.showRemainDetail(currentUser);
                        break;
                    case 3:
                        cardUtil.printConsumInfo(currentUser);
                        break;
                    case 4:
                        changePackage();
                        break;
                    case 5:
                        cancelNetwork();
                        return; // 退网后直接返回上一级
                    default:
                        System.out.println("输入无效,请输入1-5！");
                }
            } catch (NumberFormatException e) {
                currentUser = null;
                return; // 返回上一级
            }
        } while (true);
    }
    // 用户注册
    public void userRegister() {
        System.out.println("\no(=•ェ•=)m 用户注册 o(=•ェ•=)m");
        String[] numbers = cardUtil.getNewNumbers(9);
        for (int i=0;i<numbers.length; i++) {
            System.out.print((i + 1) +"."+ numbers[i]+"\t");
            if ((i+1)%3==0) System.out.println();
        }
        System.out.print("请输入您心仪的手机号(输入1-9的序号):");
        String n;
        while (true){
        try {
            n = scanner.nextLine().trim();
            Integer n1=Integer.parseInt(n);
            if (n1<1||n1>9) {
                System.out.println("输入无效！请重新输入:");
                continue;
            }
        } catch (NumberFormatException e) {
            System.out.println("请输入数字！");
            return;
        }break;}
        Integer n2=Integer.parseInt(n);
        String phone=numbers[n2 - 1];
        MobileCard newCard=new MobileCard();
        newCard.setCardNumber(phone);
        System.out.print("请输入姓名:");
        newCard.setUserName(scanner.nextLine().trim());
        System.out.print("请输入密码:");
        newCard.setPassWord(scanner.nextLine().trim());
        System.out.println("请选择您心仪的套餐:");
        cardUtil.showDescription();
        System.out.print("请输入套餐编号(1-3):");
        double price=0;
        ServicePackage pack=null;
        while (true){
            String packNum = scanner.nextLine().trim();
        switch (packNum) {
            case "1":
                pack = new TalkPackage(58);
                price = 58;
                break;
            case "2":
                pack = new NetPackage(68);
                price = 68;
                break;
            case "3":
                pack = new SuperPackage(78);
                price = 78;
                break;
            default:
                System.out.println("无效的套餐编号！");
                System.out.println("请重新输入:");
                continue;
        }break;}
        double moneyInput;
        do {
            System.out.print("请输入预存的话费金额(至少"+price+"元):");
            try {
                moneyInput = scanner.nextDouble();
                scanner.nextLine();
                if (moneyInput < price) {
                    System.out.println("您充值的钱太少啦！至少需要" +price+ "元");
                }
            } catch (NumberFormatException e) {
                System.out.println("请输入有效的金额！");
                moneyInput = 0;
            }
        } while (moneyInput<price);
        newCard.setSerPackage(pack);
        newCard.setMoney(moneyInput-price);
        cardUtil.addCard(newCard);
        System.out.println("\no(=•ェ•=)m恭喜你注册成功!o(=•ェ•=)m");
        System.out.println("卡号:" + newCard.getCardNumber());
        System.out.println("用户名:" + newCard.getUserName());
        System.out.println("当前余额:" + newCard.getMoney() + "元");
        pack.showInfo();
        cardUtil.output();
    }
    // 使用搜一搜
    public void useSoso() {
        String number;
        if (currentUser == null) {
            System.out.print("\n请输入手机卡号:");
            number = scanner.next();
            if (!cardUtil.isExistCard(number)) {
                System.out.println("卡号不存在！");
                return;
            }
        } else {
            number=currentUser;
        }
        cardUtil.useSoso(number);//通过号码使用soso服务
    }
    //充值
    public void chargeMoney() {
        String number;
        if (currentUser==null) {
            System.out.print("请输入手机卡号:");
            number = scanner.nextLine().trim();
            if (!cardUtil.isExistCard(number)) {
                System.out.println("卡号不存在！");
                return;
            }
        } else {
            number=currentUser;
        }
        double money=0;
        do {
            System.out.print("请输入充值金额(至少50元):");
            try {
                money = scanner.nextDouble();
                scanner.nextLine();
                if (money < 50) {
                    System.out.println("充值金额至少50元！");
                }
            } catch (NumberFormatException e) {
                System.out.println("请输入有效的金额！");
                money = 0;
            }
        } while (money < 50);
            cardUtil.chargeMoney(number, money);
            cardUtil.output();
    }
    // 资费说明
    public void showDescription() {
        System.out.println("\n请选择资费说明来源:");
        System.out.println("1.从文件读取  2.显示默认说明");
        System.out.print("请选择:");
        String choice = scanner.nextLine().trim();
        if ("1".equals(choice)) {
            cardUtil.showDescriptionFromFile();
        } else {
            cardUtil.showDescription();
        }  cardUtil.output();
    }
    // 套餐变更
    public void changePackage() {
        cardUtil.showDescription();
        System.out.print("请输入要变更的套餐编号(1-3):");
        String packNum = scanner.nextLine().trim();
        cardUtil.changingPack(currentUser, packNum);
        cardUtil.output();
    }
    // 办理退网
    public boolean cancelNetwork() {
        System.out.print("\n确认要办理退网吗？(y/n)：");
        String confirm = scanner.nextLine().trim();
        if (confirm.equalsIgnoreCase("y")) {
            cardUtil.delCard(currentUser);
            System.out.println("退网办理成功！");
            currentUser = null;
            return true;
        } else {
            System.out.println("已取消退网操作");
            return false;
        }
    }
    public static void main(String[] args) {
        MobileBusinessHall businessHall = new MobileBusinessHall();
        businessHall.mainMenu();
    }
}